package com.benjaminwan.read.free.model.bean

import com.benjaminwan.read.free.utils.HiUtils

data class Contents(
    val list: MutableList<ContentAbs> = mutableListOf(),
    private var lastTextIdx: Int = -1,
    private var newString: Boolean = true
) {
    @JvmOverloads
    fun addText(
        inText: String?,
        textStyle: TextStyle? = null
    ) {
        var text = inText
        if (textStyle != null) text = textStyle.toHtml(text)
        if (newString) {
            val ct = ContentText(text!!)
            list.add(ct)
            lastTextIdx = list.size - 1
            newString = false
        } else {
            val ct = list[lastTextIdx] as ContentText
            ct.append(text)
        }
    }

    fun addCode(inText: String) {
        list.add(ContentCode(inText))
    }

    fun addNotice(inText: String?) {
        var text = inText
        val ts =
            TextStyle()
        ts.color = "Gray"
        text = ts.toHtml(text)
        addText(text, ts)
    }

    fun addAppMark(text: String?, url: String?) {

    }

    fun addLink(inText: String, inUrl: String) {
        var url = inUrl
        val link: String
        if (!url.toLowerCase().startsWith("http://")
            && !url.toLowerCase().startsWith("https://")
        ) {
            url = "http://$url"
            link = " <a href=\"$url\">$inText</a> "
        } else {
            link = "[<a href=\"$url\">$inText</a>]"
        }
        if (newString) {
            list.add(ContentText(link))
            lastTextIdx = list.size - 1
            newString = false
        } else {
            val ct = list[lastTextIdx] as ContentText
            ct.append(link)
        }
    }

    fun addEmail(email: String) {
        val link = " <a href=\"mailto:$email\">$email</a> "
        if (newString) {
            list.add(ContentText(link))
            lastTextIdx = list.size - 1
            newString = false
        } else {
            val ct = list[lastTextIdx] as ContentText
            ct.append(link)
        }
    }

    fun addImg(url: String) {
        addImg(ContentImg(url, 0, ""))
    }

    fun addImg(contentImg: ContentImg) {
        list.add(contentImg)
        newString = true
    }

    fun addAttach(
        url: String?, title: String, desc: String?
    ) {
        list.add(
            ContentAttach(
                url!!,
                unEscapeHtml(title),
                desc!!
            )
        )
        newString = true
    }

    fun addAttachInfo(text: String) {
        list.add(ContentAttachInfo(text))
    }

    fun addQuote(
        text: String, authorAndTime: String, tid: String, postId: String
    ) {
        list.add(
            ContentQuote(
                text,
                authorAndTime,
                tid,
                postId
            )
        )
        newString = true
    }

    fun addGoToFloor(
        text: String, tid: String, postId: String, floor: Int, author: String
    ) {
        list.add(
            ContentGoToFloor(
                text,
                tid,
                postId,
                floor,
                author
            )
        )
        newString = true
    }

    val size: Int
        get() = list.size

    operator fun get(idx: Int): ContentAbs {
        return list[idx]
    }

    val copyText: String
        get() {
            val sb = StringBuilder()
            for (i in list.indices) {
                val o = list[i]
                if (o is ContentText || o is ContentQuote || o is ContentImg) sb.append(o.copyText)
            }
            return HiUtils.trim(sb.toString())
        }

    val content: String
        get() {
            val sb = StringBuilder()
            for (i in list.indices) {
                val o = list[i]
                if (o is ContentText) sb.append(o.content)
                if (o is ContentImg) sb.append("<i>[图片]</i>")
            }
            return sb.toString()
        }

    private fun unEscapeHtml(s: String): String {
        var str = s
        str = str.replace("&nbsp;".toRegex(), " ")
        str = str.replace("&quot;".toRegex(), "\"")
        str = str.replace("&amp;".toRegex(), "&")
        str = str.replace("&lt;".toRegex(), "<")
        str = str.replace("&gt;".toRegex(), ">")
        return str
    }

}